package com.mlk.engSystem.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.mlk.busPram.constant.BusParamConstant;
import com.mlk.busPram.mapper.EngBusParamMapper;
import com.mlk.busPram.po.EngBusParam;
import com.mlk.constant.EngConstant;
import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.exception.GlobalException;
import com.mlk.core.util.FileUploadUtils;
import com.mlk.core.util.ServerResponse;
import com.mlk.core.util.UUIDUtils;
import com.mlk.core.util.validate.ValidateUtils;
import com.mlk.engSystem.mapper.EngPicBaseAddressMapper;
import com.mlk.engSystem.mapper.EngPicBaseMapper;
import com.mlk.engSystem.po.EngPicBase;
import com.mlk.engSystem.po.EngPicBaseAddress;
import com.mlk.engSystem.service.EngPicBaseService;
import com.mlk.engSystem.vo.EngPicBaseVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class EngPicBaseServiceImpl implements EngPicBaseService {

    @Resource
    private EngPicBaseMapper engPicBaseMapper;
    @Resource
    private EngPicBaseAddressMapper engPicBaseAddressMapper;

    @Resource
    private EngBusParamMapper engBusParamMapper;

    @Override
    @Transactional
    public ServerResponse addEngPicBase(EngPicBase engPicBase) {
        ValidateUtils.validate(engPicBase);
        engPicBase.setCreateTime(new Date());

        // 此处追加字段的处理
        // 分类处理
        EngBusParam param = EngBusParam.builder().type(BusParamConstant.CATEGORY_TYPE).name(engPicBase.getCategory()).build();
        int count = this.engBusParamMapper.selectCount(new EntityWrapper<>(param));
        if (count == 0) {
            // 插入到业务数据表之中
            this.engBusParamMapper.insert(param);
        }

        // 品牌处理
        param = EngBusParam.builder().type(BusParamConstant.BRANCH_TYPE).name(engPicBase.getBranch()).build();
        count = this.engBusParamMapper.selectCount(new EntityWrapper<>(param));
        if (count == 0) {
            // 插入到业务数据表之中
            this.engBusParamMapper.insert(param);
        }

        // 对应型号的处理
        param = EngBusParam.builder().type(BusParamConstant.PARAM_TYPE).name(engPicBase.getRefType()).build();
        count = this.engBusParamMapper.selectCount(new EntityWrapper<>(param));
        if (count == 0) {
            // 插入到业务数据表之中
            this.engBusParamMapper.insert(param);
        }

        // 对应型号的处理
        param = EngBusParam.builder().type(BusParamConstant.PIC_BASE_PARAM_TYPE).name(engPicBase.getType()).build();
        count = this.engBusParamMapper.selectCount(new EntityWrapper<>(param));
        if (count == 0) {
            // 插入到业务数据表之中
            this.engBusParamMapper.insert(param);
        }

        //插入到主体之中
        count = this.engPicBaseMapper.insert(engPicBase);
        return count == 0 ? ServerResponse.fail("添加失败") : ServerResponse.ok("添加成功");
    }

    @Override
    public ServerResponse addEngPicBaseAddress(EngPicBaseAddress engPicBaseAddress, MultipartFile file) {
        FileUploadUtils.UploadContext context = null;
        String path = EngConstant.PicPath.SYSTEM_PIC_BASE;
        if ((context = FileUploadUtils.uploadContext(file, path, UUIDUtils.uuid())).getSuccess()) {
            engPicBaseAddress.setUrl(context.getSaveFullPath());
            int count = this.engPicBaseAddressMapper.insert(engPicBaseAddress);
            return count == 0 ? ServerResponse.fail("添加失败") : ServerResponse.ok("添加成功", engPicBaseAddress);
        }
        throw new GlobalException("添加失败");
    }

    @Override
    public ServerResponse findAddEngPicBase(String allParameters, Integer page, Integer limit) {
        List<EngPicBaseVO> lists = null;
        try {
            PageHelper.startPage(page, limit);
            lists = this.engPicBaseMapper.findAddEngPicBase(allParameters);
        } finally {
            PageHelper.freeTotal();
        }

        lists.forEach(e -> {
            e.setItms(this.engPicBaseAddressMapper.selectList(new EntityWrapper<>(EngPicBaseAddress.builder().uniCode(e.getUniCode()).build())));
        });

        return ServerResponse.ok(lists);
    }

    /**
     * 删除图片
     *
     * @param id
     * @return
     */
    @Override
    public ServerResponse delPic(Integer id) {
        int count = this.engPicBaseAddressMapper.deleteById(id);
        if (count == 0) {
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    /**
     * 分页数据展示
     *
     * @param base
     * @return
     */
    @Override
    @Page
    public Pager listPage(EngPicBase base) {
        return Pager.adapt(this.engPicBaseMapper.selectForPage(base));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ServerResponse del(Integer id) {
        int count = this.engPicBaseMapper.deleteById(id);
        if (count == 0) {
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    /**
     * 查看图片
     * @param uniCode
     * @return
     */
    @Override
    public ServerResponse lookPic(String uniCode) {
        return ServerResponse.ok(this.engPicBaseAddressMapper.selectList(new EntityWrapper<>(EngPicBaseAddress.builder().uniCode(uniCode).build())));
    }

    @Override
    public ServerResponse updateEngPicBase(EngPicBase engPicBase) {
        int count = this.engPicBaseMapper.updateById(engPicBase);
        if(count == 0){
            return ServerResponse.fail("更新失败!");
        }
        return ServerResponse.ok("更新成功!");
    }

}
